#include "tools.h"
#include "SearchKernel.cuh"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <algorithm>

using namespace std;

void printResultTracks(char* results){
	int trackNum = ((int*)results)[0];
	int hitsNum = ((int*)results)[1];

	hitData *hits = (hitData*)(results+2*sizeof(int));
	track *tr = (track*)(hits+hitsNum);
	for (int i =0; i<trackNum ; ++i){ //for each track
		printf("=====Track Nb %i (counting from 1) =====\n ", tr[i].internalId+1);
		for (int j = tr[i].firstHit ; j< (tr[i].firstHit + tr[i].trackHitsNum) ; j++){
			hitData tmp = hits[j];
			printf("id %i sensor %i x %f y %f z %f \n", tmp.id, tmp.sensor, tmp.x, tmp.y, tmp.z);
		}
	}
}

//TODO: add mutexes and interprocess conditions for synchronization with Gaudi
int main(){
	using namespace boost::interprocess;
	try{
		managed_shared_memory segment
		 (open_only
		 ,"gpuSharedMemory");

		typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;

		typedef vector<int, ShmemAllocator> MyVector;

		MyVector *myvector = segment.find<MyVector>("inputData").first;

		//sortHits
		//launchKernel

		segment.destroy<MyVector>("inputData");
	   }
	   catch(...){
	      shared_memory_object::remove("gpuSharedMemory");
	      throw;
	   }
	   shared_memory_object::remove("gpuSharedMemory");
	   return 0;

}
